package com.cnhis.cloudhealth.clinical.hosnurse.executionorder.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.cnhis.cloudhealth.commons.utils.*;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosdoctor.doctorparameter.bo.DoctorparameterBo;
import com.cnhis.cloudhealth.clinical.hosnurse.executionorder.dao.HosExecutionOrderDao;
import com.cnhis.cloudhealth.clinical.hosnurse.hossendadvice.bo.HosSendAdviceBo;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;

import net.sf.json.JSONObject;

@SuppressWarnings({"unchecked", "rawtypes"})
@Service
public class HosExecutionOrderBo
{
    @Resource
    private HosExecutionOrderDao hosExecutionOrderDao;
    @Autowired
    GetNewId getNewId;
    @Autowired
	private DoctorparameterBo DoctorparameterBo;
	@Autowired
    private HosSendAdviceBo HosSendAdviceBo;
    public ModelVo patientList(Map map, int currPage, int pageSize)
        throws Exception
    {
        if ("0".equals(map.get("lType").toString()) && "0".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 0);
            map.put("lVBI29B", 2);
            
        }
        if ("0".equals(map.get("lType").toString()) && "1".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 1);
            map.put("lVBI29B", 3);
            
        }
        if ("1".equals(map.get("lType").toString()) && "0".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 0);
            map.put("lVBI29B", 1);
            
        }
        if ("1".equals(map.get("lType").toString()) && "1".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 2);
            map.put("lVBI29B", 3);
            
        }
        if ("2".equals(map.get("lType").toString()) && "0".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 0);
            map.put("lVBI29B", 0);
        }
        if ("2".equals(map.get("lType").toString()) && "1".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 1);
            map.put("lVBI29B", 1);
        }
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = hosExecutionOrderDao.patientList(map, currPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo examine(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = new Page();
        try
        {
            
            pg = hosExecutionOrderDao.examine(map, currPage, pageSize);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
        
    }
    
    public ModelVo checkOut(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = new Page();
        try
        {
            
            pg = hosExecutionOrderDao.checkOut(map, currPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo areaPatientList(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            
            ls = hosExecutionOrderDao.areaPatientList(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(ls);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(ls);
        return mo;
    }
    
    public ModelVo queryItems()
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            Map map = new HashMap();
            map.put("aDate", DateUtils.getDateStr(new Date()) + " 23:59:59");
            ls = hosExecutionOrderDao.queryItems(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(ls);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(ls);
        return mo;
        
    }
    
    public ModelVo queryAdviceList(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = new Page();
        try
        {
            Object lxml = map.get("lxml");
            if (lxml == null || "".equals(lxml))
            {
                mo.setResult(StaticKeys.OH_FALSE);
                mo.setResultMsg("查询参数不能为空");
                return mo;
                
            }
            JSONObject object = JSONObject.fromObject(lxml);
            List<Map<String, Object>> TmpVAE = JsonUtil.getList(object.getJSONArray("VAE1").toString());
            List<Map<String, Object>> TmpBBX = JsonUtil.getList(object.getJSONArray("BBX1").toString());
            map.put("TmpVAE", TmpVAE);
            map.put("TmpBBX", TmpBBX);
            TmpVAE = hosExecutionOrderDao.getTmp_VAE(map);
            map.put("TmpVAE", TmpVAE);
            
            pg = hosExecutionOrderDao.queryAdviceList(map, currPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }

    public ModelVo queryBarcodeInfo(Map map)throws Exception{
        ModelVo mo = new ModelVo();
        List ls = null;
        try{
            ls = hosExecutionOrderDao.getBarcodeInfo(map);
        }catch (Exception e){
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(ls);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(ls);
        return mo;

    }
    public ModelVo insertExeRecord(Map map)throws Exception{
        ModelVo mo = new ModelVo();
        String lxml = MapUtils.getString(map,"lxml");
        if(StringUtils.isBlank(lxml)){
            return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，医嘱执行记录相关信息为空！");
        }
        List<Map<String,Object>> list = JsonUtil.getList(lxml);
        for(Map<String,Object> pm:list){
            Long exe01 = getNewId.nextId();
            pm.put("exe01",exe01);
        }
        map.put("exeList",list);
        hosExecutionOrderDao.insertExeRecord(map);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setResultMsg("插入记录成功");
        return mo;
    }

    public ModelVo updateExeRecord(Map map)throws Exception{
        ModelVo mo = new ModelVo();
        hosExecutionOrderDao.updateExeRecord(map);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setResultMsg("更新状态成功");
        return mo;
    }

    public ModelVo queryUnExeRecord(Map map,int pageno,int pagesize)throws Exception{
        return MySqlUtil.packModelVoPage("HosExecutionOrder.getUnExecuteOrder",map,pageno,pagesize,hosExecutionOrderDao);
    }
    public ModelVo queryExeRecord(Map map,int pageno,int pagesize)throws Exception{
        return MySqlUtil.packModelVoPage("HosExecutionOrder.getExecuteOrder",map,pageno,pagesize,hosExecutionOrderDao);
    }

    public ModelVo patientList1(Map map)
    {
        if ("0".equals(map.get("lType").toString()) && "0".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 0);
            map.put("lVBI29B", 2);
            
        }
        if ("0".equals(map.get("lType").toString()) && "1".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 1);
            map.put("lVBI29B", 3);
            
        }
        if ("1".equals(map.get("lType").toString()) && "0".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 0);
            map.put("lVBI29B", 1);
            
        }
        if ("1".equals(map.get("lType").toString()) && "1".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 2);
            map.put("lVBI29B", 3);
            
        }
        if ("2".equals(map.get("lType").toString()) && "0".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 0);
            map.put("lVBI29B", 0);
        }
        if ("2".equals(map.get("lType").toString()) && "1".equals(map.get("rPrint").toString()))
        {
            map.put("lVBI29A", 1);
            map.put("lVBI29B", 1);
        }
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = hosExecutionOrderDao.patientList1(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
//        mo.setList(list);
        mo.setTotal(list.size());
        mo.setRows(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo examine1(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosExecutionOrderDao.examine1(map);
            String type = checklisbill(list);
    		if(type.equals("true")){
    			list = hosExecutionOrderDao.examine1(map);
    		}
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setRows(list);
            return mo;
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
    }
  //检验是否有条码号
  	public String checklisbill(List<?> list) throws Exception{
  		if(DoctorparameterBo.getSysValue(100,105001,394).equals("1")){
  			int tof = 0;
  			String lXml = "{\"VAF1\":[";
  			Long lBCE01 = 0l; //员工ID
  			Long lBCE02 = 0l; //当前操作员编号
  			String lBCE03 = "";  //当前操作员姓名
  			if(list!=null&&list.size()>0){
  				for(int i=0;i<list.size();i++){
  					Map map = (Map) list.get(i);
  					String vbi21 = (String) map.get("vbi21");
  					int vaf11 = MapUtils.getInteger(map,"vaf11",0);
  					if(vbi21.equals("") && vaf11==2){
  						Long vaf01 = MapUtils.getLong(map,"vaf01",0l);
  						Long vaa01 = MapUtils.getLong(map,"vaa01",0l);
  						lBCE01 = MapUtils.getLong(map,"bce01"); //员工ID
  						lBCE02 = MapUtils.getLong(map,"bce02"); //当前操作员编号
  						lBCE03 = MapUtils.getString(map,"bce03");  //当前操作员姓名
  						if(tof>0){
          					lXml+=",";
          				}
          				lXml+="{\"vaf01\":\""+vaf01+"\",\"vaa01\":\""+vaa01+"\",\"vaf11\":"+vaf11+"}";
          				tof++;						
  					}
  				}
  				lXml+="]}";
  				if(tof>0){
  					Map map1 = new HashMap();
  					map1.put("lXml", lXml);
  					map1.put("lBCE01", lBCE01);
  					map1.put("lBCE02", lBCE02);
  					map1.put("lBCE03", lBCE03);
  					map1.put("lACF01", 2);
  					HosSendAdviceBo.nurseLisBillsPrintAuto(map1);
  					return "true";
  				}
  			}			
  		}	
  		return "false";
  	}

}
